package org.example.web;


import com.example.fanqie.entity.Author;
import com.example.fanqie.entity.Category;
import com.example.fanqie.entity.Chapter;
import com.example.fanqie.entity.Novel;
import com.example.fanqie.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
@RestController
public class NovelList {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private NovelMapper novelMapper;

    @Resource
    private AuthorMapper authorMapper;
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private BookshelfMapper bookshelfMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @PostConstruct
    public void redisInit() {
        // 将自己的业务数据先存入Redis中完成预热
        List<Novel>  novels = novelMapper.selectnovel();
        for (Novel novel : novels) {
            stringRedisTemplate.opsForZSet().incrementScore("product:rank", String.valueOf(novel.getNovelId()), 0);

        }

    }

    //点击  热度加1
    @RequestMapping("diji")
    public Novel getInfo(@RequestParam("novelId") Integer novelId) {
        stringRedisTemplate.opsForZSet().incrementScore("product:rank", novelId.toString(), 1);
        return novelMapper.selectnovelId(novelId);
    }



    //查询总数据
    @RequestMapping("/list")
    public List<Novel> wxList() {


        Set<ZSetOperations.TypedTuple<String>> hotnessRanking = stringRedisTemplate.opsForZSet().reverseRangeWithScores("product:rank", 0, 9);

        List<Novel> resultList = new ArrayList<>();
        for (ZSetOperations.TypedTuple<String> tuple : hotnessRanking) {
            String productId = tuple.getValue();


            // 根据 productId 查询 TzBusinessGoods 对象，这里假设有一个方法 getProductById 可以根据 productId 获取对应的 TzBusinessGoods 对象
            Novel novel1 = novelMapper.selectnovelId( Integer.parseInt(productId) );


            if ( novel1 != null) {
                // 设置商品的热度分数
                resultList.add(novel1);
            }
        }

        for (Novel novel : resultList) {
            Author author = authorMapper.selectauthorId(novel.getAuthorId());
            Category category = categoryMapper.selectcategoryId(novel.getCategoryId());
            Integer integer = bookshelfMapper.selectCount(novel.getNovelId());
            List<Chapter> chapters = chapterMapper.selectchapterlist(novel.getNovelId());

            novel.setAuthorName(author.getAuthorName());
            novel.setCategoryName(category.getCategoryName());
            novel.setReaders(integer);
            if(chapters.size() > 0){
                novel.setChapterTitle(chapters.get(chapters.size()-1).getChapterTitle());
                novel.setPublishDatezuixin(chapters.get(chapters.size()-1).getPublishDate());
            }


        }

        System.out.println(resultList);
        return resultList;
    }



}
